/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gatewinapp;

import gate.Corpus;
import gate.DataStore;
import gate.Document;
import gate.Factory;
import gate.creole.ResourceInstantiationException;
import gate.creole.ir.QueryResult;
import gate.creole.ir.QueryResultList;
import gate.creole.ir.Search;
import gate.util.Err;
import gate.util.Out;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.text.html.HTML;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import org.xml.sax.SAXException;

/**
 *
 * @author Thach
 */
public class InformationRetrievalApp {
    
    //Thư mục gốc
    private static final String ROOT_PATH = "C:/gate-7-2/GateWinApp";
    
    //Thư mục lưu trữ index
    private static String INDEX_LOCATION = ROOT_PATH + "/lucene";
  
    //Thư mục lưu trữ datastore
    private static String SERIAL_DATASTORE_PATH = "file:///" + ROOT_PATH + "/gate_corpus";
    private static String SERIAL_DATASTORE_FOLDER = ROOT_PATH + "/gate_corpus";
    
    //Thư mục lưu trữ document
    private static String DOCUMENTS_PATH = ROOT_PATH + "/document";
    
    public InformationRetrievalApp() {
    }
    
    public String timKiem2(DataStore sds, Search search, Query query)
    {
        HTMLDoc hres = new HTMLDoc();
        hres.setTitle("Search Result");
        try {
            //Thực thi câu truy vấn
            QueryResultList res = search.search(query.createQueryString());
            //Trả về kết quả
            Iterator it = res.getQueryResults();
            //Xem kết quả
            boolean haveNoResult = true;
            while (it.hasNext()) {
                haveNoResult = false;
                QueryResult qr = (QueryResult) it.next();
                Document resultDoc = (Document) sds.getLr("gate.corpora.DocumentImpl", qr.getDocumentID());
                hres.addDoc(resultDoc.getName(), resultDoc.getSourceUrl(), resultDoc.getContent().toString(), query.getArrayQuery());
            }
            if (haveNoResult) {
                hres.setContent("Không tìm thấy kết quả nào!");
            }
        }
        catch(Exception ex)
        {
            ex.printStackTrace(Err.getPrintWriter());
        }
        return hres.genDoc();
    }
    
    public void timKiem(DataStore sds, Search search, String query) throws ParserConfigurationException, SAXException, IOException
    {
      try
      {
      //Thực thi câu truy vấn
      QueryResultList res = search.search(query);
      //Trả về kết quả
      Iterator it = res.getQueryResults();
      //Xem kết quả
      while (it.hasNext()) {
        QueryResult qr = (QueryResult) it.next();
        float score = qr.getScore();
        //
        //  Lấy thông tin về tài liệu liên quan
        Document resultDoc = (Document) sds.getLr("gate.corpora.DocumentImpl", qr.getDocumentID());
        Out.prln("Query: Tên tài liệu = " + resultDoc.getName()); 
        Out.prln("Query: Tần suất xuất hiện  = " + score);
        
        // Output kết quả tìm kiếm ra file XML, lưu tại project thực thi
        File file = new File("xml/" + resultDoc.getName().substring(0, resultDoc.getName().length() - 4)+".xml");
        // if file doesnt exists, then create it
	if (!file.exists()) {
                    file.createNewFile();
	}
        
        FileWriter writer = new FileWriter(file.getAbsoluteFile());
        writer.write(resultDoc.toXml());
        writer.close(); 
        }
        
        Out.prln("------------");
      }
      catch(Exception ex)
      {
          ex.printStackTrace(Err.getPrintWriter());
      }
  }
     
  public void searchNode(String[] array_query) throws ParserConfigurationException, SAXException, IOException, TransformerConfigurationException, TransformerException
  {
        File folder = new File("xml");
        File[] listFile = folder.listFiles();
        for(int k = 0; k< listFile.length; k++){
            if(listFile[k].isFile()){
                
                File file  = new File(listFile[k].getPath());
                CreateXML xml = new CreateXML(file);
                String text = xml.getTextNode();
        
                xml.removeAnnotationSet();
                
                for(int j = 0; j<array_query.length; j++){
                    FormatText ft = new FormatText(text);

                    String[] left_str = ft.leftContent(array_query[j]);
                    String[] right_str = ft.rightContent(array_query[j]);
                    int count = ft.countFrequency(array_query[j]);
                    for(int i = 0; i< count; i++){
                        xml.createElementResult(count, left_str[i], array_query[j], right_str[i]);
                    }

                    xml.createFileResult();
                }
            }
        }
  }
  
  public Corpus createTestCorpus()
    throws MalformedURLException, ResourceInstantiationException {
    //Tạo 1 corpus với những tài liệu trên
    Corpus result = Factory.newCorpus("test corpus");
    assert result != null;
    
    File folder = new File(DOCUMENTS_PATH);
    result = addDoctoCorpus(result, folder);
    return result;
  }
  
  private Corpus addDoctoCorpus(Corpus corpus, File folder)
    throws MalformedURLException {
      folder = normalizeFileName(folder);
      if (!folder.isDirectory()) {
          return corpus;
      }
      // if folder is really a folder, not a file
      File[] listFile = folder.listFiles();
      for (int i = 0; i < listFile.length; i++) {
          listFile[i] = normalizeFileName(listFile[i]);
          if (listFile[i].isDirectory()) {
              corpus = addDoctoCorpus(corpus, listFile[i]);
          } else {
              String path = "file:///" + listFile[i].getPath();
              URL url = new URL(path);
              Document doc;
              try {
                  doc = makeNewDoc(url);
                  corpus.add(doc);
              } catch (ResourceInstantiationException ex) {
                  Logger.getLogger(InformationRetrievalApp.class.getName()).log(Level.SEVERE, null, ex);
              }
          }
      }
      return corpus;
  }
  
  private File normalizeFileName(File file) {
      String name = file.getPath();
      name = name.replace(" ", "_");
      name = name.replace("%", "_");
      name = name.replace("#", "_");
      File newFile = new File(name);
      boolean s = file.renameTo(newFile);
      if(s) {
        return newFile;
      }
      else
      {
        return file;
      }
  }
  
  private Document makeNewDoc(URL url) throws ResourceInstantiationException {
      Document doc;
      doc = Factory.newDocument(url);
      String content = doc.getContent().toString();
      int i = content.indexOf("\n");
      String title = content.substring(0, i);
      
      doc.getFeatures().put("title", title);
      doc.setName(title);
      return doc;
  }

  public String getIL() {
      return INDEX_LOCATION;
  }
  

  public String getSDP() {
      return SERIAL_DATASTORE_PATH;
  }
  
  public String getSDF() {
      return SERIAL_DATASTORE_FOLDER;
  }
}